package com.wibo.account;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.wibo.account.R;
import com.wibo.common.Utility;

import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceActivity;
import android.provider.MediaStore;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

public class WiboSettings extends PreferenceActivity {
	
	private static String mImageName = "default.jpg";

	/* (non-Javadoc)
	 * @see android.preference.PreferenceActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		addPreferencesFromResource(R.xml.settings);
	}

	public Uri getTempUri() {
		return Uri.fromFile(getTempFile());
	}
	
	public File getTempFile() {
		if (Utility.isSDCARDMounted()) {
			
			File f = new File(Environment.getExternalStorageDirectory() + "/Camera/cut_wallpaper/", mImageName);
			try {
				File destDir = new File(Environment.getExternalStorageDirectory() + "/Camera/cut_wallpaper/");
				if (!destDir.exists()) {
					destDir.mkdirs();
				}
				f.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				//Toast.makeText(thiz, R.string.fileIOIssue, Toast.LENGTH_LONG).show();
			}
			return f;
		} else {
			return null;
		}
	}
	
	/* (non-Javadoc)
	 * @see android.preference.PreferenceActivity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	
		if (requestCode == 0) {
			if (resultCode != RESULT_OK) {
				mImageName = "default.jpg";
				return;
			}else{
				if (data != null) {
					SharedPreferences settings = getSharedPreferences(
							WiboMainActivity.USER_INFO, 0);
					Editor editor = settings.edit();
					editor.putString(WiboMainActivity.LOADING_IMAGE_PATH, getTempFile().getAbsolutePath());
					editor.commit();
					Toast.makeText(this, getString(R.string.loadingPicSuc), Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

	public void setImageName(String path) {
		mImageName = path;
	}
	
	public String getImageName() {
		return mImageName;
	}
}
